package com.tn.service.rpm.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.tn.cache.VoiceWarningStatusCache;
import com.tn.controller.alarm.common.constants.RpmConstants;
import com.tn.controller.alarm.common.enums.RpmEventTypeEnum;
import com.tn.controller.alarm.common.model.rpm.mq.EventMQDTO;
import com.tn.controller.alarm.common.model.rpm.mq.RpmMqDTO;
import com.tn.dao.rpm.IEventLogDAO;
import com.tn.mqtt.AbstractBaseCallback;
import com.tn.service.tallying.TallyingService;
import com.tn.service.vehicle.IVehicleService;
import com.tn.service.voice.SendVoiceDataMQ;
import com.tn.service.voice.VoiceProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName RpmAsyncMq
 * @Description TODO
 * @Author Yezhongzhu
 * @Date 2021/12/27 18:05
 * @Version v1.0
 **/
@Component
@Slf4j
public class RpmAsyncMQ extends AbstractBaseCallback {
    private final static  String topic = "rpm_serve/async";
    @Autowired
    private IEventLogDAO iEventLogDAO;
    @Autowired
    private IVehicleService service;
    @Autowired
    private TallyingService tallyingService;
    @Autowired
    private VoiceWarningStatusCache cache;
    @Autowired
    private SendVoiceDataMQ sendVoiceDataMQ;
    @Autowired
    private VoiceProperties properties;

    @Override
    public void consumer(String topic, String msg) {
        log.info("topic:{}, 消息是 msg:{}",topic,msg);
        if(StringUtils.isBlank(msg)){
            log.error("RpmAsyncMQ msg empty {}",msg);
            return;
        }
        //TODO业务逻辑
        RpmMqDTO<EventMQDTO> eventMQDTORpmMqDTO = JSON.parseObject(msg,new TypeReference<RpmMqDTO<EventMQDTO>>(){});
        if(null==eventMQDTORpmMqDTO||null==eventMQDTORpmMqDTO.getEvent()){
            log.error("RpmAsyncMQ eventMQDTORpmMqDTO or eventMQDTORpmMqDTO.getEvent empty msg：{}",msg);
            return;
        }
        int type = iEventLogDAO.getTypeById(eventMQDTORpmMqDTO.getRequestId());
        RpmEventTypeEnum rpmEventTypeEnum = RpmEventTypeEnum.getByCode(type);
        if(null==rpmEventTypeEnum){
            log.error("RpmAsyncMQ rpmEventTypeEnum do not know type:{}",type);
            return;
        }
        switch (rpmEventTypeEnum){
            case STATICFENCE:
                //告警信息处理
                EventMQDTO eventMQDTO = eventMQDTORpmMqDTO.getEvent();
                if(CollectionUtils.isEmpty(eventMQDTO.getObjects())){
                    log.error("RpmAsyncMQ eventMQDTO.getObjects() is empty:{}",msg);
                    return;
                }
                String tagId = eventMQDTO.getObjects().get(0);
                if(RpmConstants.FANCE_OUT==eventMQDTO.getType()){
                    service.vehicleAlarmDataHandle(eventMQDTO);
                    sendVoiceWarning(tagId);
                }else{
                    cache.brakeWarningStatus(tagId);
                }
                break;
            case RADIUS:
                //理货信息判断
                tallyingService.tallyingHandle(eventMQDTORpmMqDTO.getEvent());
                break;
            default:
                break;
        }
    }

    private void sendVoiceWarning(String tagId) {
        sendVoiceDataMQ.sendVoiceTextData(tagId,properties.getWarnVehicleText());
        cache.addWarningStatus(tagId);
        int timeInterval =  properties.getTimeInterval();
        while (cache.getWarnContinue()==cache.getWarningStatus(tagId)){
            try {
                Thread.sleep(timeInterval);
            } catch (InterruptedException e) {
                log.error("RpmAsyncMQ sleep error:{}",e.getMessage(),e);
            }
            sendVoiceDataMQ.sendVoiceTextData(tagId,properties.getWarnVehicleText());
        }
    }

    @Override
    protected AbstractBaseCallback getClazz() {
        return this;
    }

    @Override
    protected String getTopic() {
        return topic;
    }
}
